//
//  GiftPageController.m
//  MindBox
//
//  Created by MS on 16-1-8.
//  Copyright (c) 2016年 Lemon. All rights reserved.
//

#import "GiftPageController.h"
#import "GoodsListController.h"
#import "GoodsController.h"
#import "GoodsDetailController.h"
#import "RaidersDetailController.h"
#import "GiftPageDataTool.h"
#import "GiftPageView.h"
@interface GiftPageController ()<GiftPageViewDelegate>
@property (nonatomic,weak) UIImageView * titleView;
@property (nonatomic,weak) GiftPageView * giftPageView;
@property (nonatomic,strong) NSMutableArray * itemGoodsArray;
@property (nonatomic,copy) NSString * gos_id;
@property (nonatomic,assign) int pages;
@end

@implementation GiftPageController

-(GiftPageView *)giftPageView
{
    if (_giftPageView==nil) {
        GiftPageView * view = [[GiftPageView alloc] initWithFrame:CGRectMake(0, 64, self.view.width, self.view.height-64)];
        [self.view addSubview:view];
        _giftPageView = view;
        _giftPageView.delegate = self;
    }
    return _giftPageView;
}

-(NSMutableArray *)itemGoodsArray
{
    if (_itemGoodsArray==nil) {
        _itemGoodsArray = [NSMutableArray array];
    }
    return _itemGoodsArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self setNavBar];
    
    [self loadgiftPageData];
    [self loadgiftPage_Sel_Data];
 
    [self addHeaderRefresh];
    [self addFootRefresh];
}


-(void)loadgiftPageData
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    [GiftPageDataTool giftPageDataWithCat_id:self.cat_id Success:^(GiftPageData *data){
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        [self.titleView sd_setImageWithURL:[NSURL URLWithString:data.title_url]];
        self.giftPageView.giftPageData = data;

       
   } failure:^(NSError *error) {
       
   }];
    
    
}

-(void)loadgiftPage_Sel_Data
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    [GiftPageDataTool giftPage_Sel_DataWithCat_id:self.cat_id Success:^(NSArray *data) {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
         [_giftPageView.collectionView.mj_header endRefreshing];
        
        self.giftPageView.gift_SelData = data;
        
    } failure:^(NSError *error) {
        
    }];

}

-(void)loadgiftPageItemGoods_Data
{
    [GiftPageDataTool giftPage_ItemGoods_DataWithCat_id:self.cat_id seach_code:self.gos_id pages:++self.pages Success:^(NSArray *data) {
        [_giftPageView.collectionView.mj_footer endRefreshing];

        
        if (data.count) {
            [self.itemGoodsArray addObjectsFromArray:data];
        }
        
        self.giftPageView.gift_ItemGoods = self.itemGoodsArray;

        
    } failure:^(NSError *error) {
        
    }];

}

-(void)addFootRefresh
{
    __weak typeof(self) weakSelf = self;
    
  self.giftPageView.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf loadgiftPageItemGoods_Data];
    }];
    
}

-(void)addHeaderRefresh
{
    __weak typeof(self) weakSelf = self;
    
   self.giftPageView.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.pages = 0;
        [weakSelf.itemGoodsArray removeAllObjects];
        [weakSelf loadgiftPageItemGoods_Data];
        [weakSelf loadgiftPage_Sel_Data];
    }];
}

-(void)setNavBar
{
    MyNavigationItem * leftItem = [MyNavigationItem navigationItemWithImage:@"left_go" andHighlightImage:@"left_go" andTag:0 andTitle:nil addTarget:self action:@selector(leftBtnClick)];
    
    MyNavigationItem * rightItem =[MyNavigationItem navigationItemWithImage:@"threedot" andHighlightImage:@"threedot" andTag:0 andTitle:nil addTarget:self action:@selector(rightClick)];
    
    MyNavigationBar * navBar = [[MyNavigationBar alloc]initWithFrame:CGRectMake(0, 20, self.view.width, 44)];
    
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 15)];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.center = CGPointMake(navBar.width*0.5, navBar.height*0.5);
    self.titleView = imageView;
    [navBar addSubview:imageView];
    
    
    UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, navBar.frame.size.height-0.5, navBar.frame.size.width, 0.5)];
    lineView.backgroundColor =[UIColor grayColor];
    [navBar addSubview:lineView];
    
    [navBar setNavigationBarWithBgImageName:@"无" andTitle:nil  andTitleView:nil  andLeftItems:@[leftItem] andRightItems:@[rightItem]];
    [self.view addSubview:navBar];

}


-(void)giftAdsViewDidClick:(GiftPageView *)giftView banner:(Gift_Banner *)banner index:(NSInteger)index
{
    if ([banner.table_name isEqualToString:@"goods"]) {
        GoodsController * vc  = [[GoodsController alloc] init];
        vc.good_id = banner.table_id;
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
    if ([banner.table_name isEqualToString:@"key_search"]) {
        GoodsListController * vc = [[GoodsListController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        vc.name_id = banner.name;
        vc.titleName = banner.name;
        [self.navigationController pushViewController:vc animated:YES];

    }
    
}

-(void)giftLabelViewDidClick:(GiftPageView *)giftView label:(Gift_Label *)label index:(NSInteger)index
{
    GoodsListController * vc = [[GoodsListController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    vc.label_id = label.label_id;
    vc.titleName = label.name;
    [self.navigationController pushViewController:vc animated:YES];
   
}

-(void)giftSelectedViewDidClick:(GiftPageView *)giftView selectItem:(Gift_Cat_Id *)cat_id index:(NSInteger)index
{
    self.gos_id = cat_id.gos_id;
    [self.itemGoodsArray removeAllObjects];
     self.pages = 0;
    [self loadgiftPageItemGoods_Data];
    
}

-(void)giftReguessGoodsViewDidClick:(GiftPageView *)giftView goods:(Gift_Goods *)goods index:(NSInteger)index
{
    GoodsController * vc  = [[GoodsController alloc] init];
    vc.good_id = goods.goods_id;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];

}

-(void)giftSelViewDidClick:(GiftPageView *)giftView selTopic:(Gift_SeleData *)seleTopic index:(NSInteger)index
{
    RaidersDetailController * vc = [[RaidersDetailController alloc] init];
    vc.urlStr = seleTopic.html_src;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)giftCollectionViewDidClick:(GiftPageView *)giftView itemGoods:(Gift_ItemGoods *)goods index:(NSInteger)index
{
    GoodsController * vc  = [[GoodsController alloc] init];
    vc.good_id = goods.goods_id;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)leftBtnClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)rightClick{
    
    
}

-(void)dealloc
{
//   NSLog(@"GiftPageController卸载") ;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
